<?php

namespace app\admin\controller\curriculum;

use app\admin\controller\Controllers;
use app\admin\service\curriculum\Teacher as TeacherService;
use app\admin\model\curriculum\Teacher as TeacherModels;

class Teacher extends Controllers
{
    /**
     * 显示资源列表
     *
     * @param TeacherService $service
     * @return \think\Response
     */
    public function index( TeacherService $service )
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\curriculum\Teacher.index');
        $list = $service->indexPaginate($get);
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total(),
        ]);
        return $this->fetch('curriculum/teacher/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return $this->fetch('curriculum/teacher/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save( TeacherService $service )
    {
        $post = $this->request->only(['head_url','name','seniority','profiles'], 'post');
        $this->validate($post, 'app\admin\validate\curriculum\Teacher.create');
        $teachid = $service->create($post);
        if ( false === $teachid ) {
            return failed('新增失败');
        }
        return success();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(TeacherModels $model)
    {
        $this->assign([
            'data' => $model
        ]);
        return $this->fetch('curriculum/teacher/edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  TeacherModels $model
     * @param  TeacherService $service
     * @return \think\Response
     */
    public function update(TeacherModels $model, TeacherService $service)
    {
        $put = $this->request->only(['head_url','name','seniority','profiles'], 'put');
        $this->validate($put, 'app\admin\validate\curriculum\Teacher.update');
        $update = $service->save($model, $put);
        if ( false === $update ) {
            return failed('编辑成功');
        }
        return success();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @param TeacherService $service
     * @return \think\Response
     */
    public function delete(int $id, TeacherService $service)
    {
        $this->validate(['id'=>$id], 'app\admin\validate\curriculum\Teacher.delete');
        if ( $service->destory($id) ) {
            return success();
        }
        return failed('删除失败');
    }

    /**
     * 批量删除
     * @author zx
     * @date 2019-12-09
     * @return \think\Response
     */
    public function destory(TeacherService $service)
    {
        $delete = $this->request->only(['ids'], 'post');
        $this->validate($delete, 'app\admin\validate\curriculum\Teacher.destory');
        if ( $service->destory($delete['ids']) ) {
            return success();
        }
        return failed('删除失败');
    }
}
